package handler

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
	"post-hub/inits"
	"post-hub/internal/dto"
	"post-hub/internal/model"
	"post-hub/response"
	"time"
)

func UpdateUser(c *gin.Context) {
	var user model.User
	err := c.ShouldBind(&user)
	if err != nil {
		response.Error(c, "解析失败")
		return
	}
	user = model.User{
		Model: gorm.Model{
			ID: user.ID,
		},
		Image:    user.Image,
		Username: user.Username,
		Tel:      user.Tel,
		Fans:     user.Fans,
		Follow:   user.Follow,
		Title:    user.Title,
	}
	err = dto.UserUpdate(&user, int(user.ID))
	if err != nil {
		response.Error(c, "更新失败")
		return
	}
	response.Success(c, "更新成功", user)
	userKey := "user"
	marshal, err := json.Marshal(user)
	if err != nil {
		return
	}
	inits.Rdb.Set(inits.Ctx, userKey, marshal, time.Minute*5)

}
